package com.kingwood.algorithm.test;

import sun.plugin.javascript.navig.Link;

import java.util.*;

/**
 * @Author 22025812
 * @Description: TODO
 * @Date: 2024/3/1 9:13
 * @Modified By：
 * @Version 1.0
 */
public class JinZhiYunSuan {

    /**
     * 来自异国的客人
     * https://fcqian.blog.csdn.net/article/details/134751635
     * @param k 表示该客人购买的物品价值（以十进制计算的价格）
     * @param n 表示该客人的幸运数字
     * @param m 表示该客人所在国度采用的进制
     * @return
     */
    public static long howmuchLuckyNum(long k, long n, long m) {
        long count = 0;
        if (n > m) {
            return count;
        }

        while (k > 0) {
            long temp = k % m;
            if (temp == n) {
                count++;
            }
            k = k / m;
        }

        return count;
    }

    public static int howmuchLuckyNum2(long k, long n, long m) {
        int count = 0;
        if (n > m) {
            return 0;
        }

        while (k > 0) {
            long temp = k % m;
            if (temp == n) {
                count++;
            }
            k = k / m;
        }

        return count;
    }

    /**
     * 靠谱的车
     * https://fcqian.blog.csdn.net/article/details/127418150
     * 原题解有误
     * @param cost
     * @return
     */
    public static int getActualCost(int cost) {
//        int correct = Integer.parseInt(String.valueOf(cost), 9);
        int correct = 0;

        ArrayList<Integer> list = new ArrayList<>();
        int tmp = cost;
        while (tmp > 0) {
            int yu = tmp % 10;
            list.add(yu);
            tmp /= 10;
        }

        for (int i=0; i<list.size(); i++) {
            int cur = list.get(i);
            if (cur > 4) {
                cur--;
            }

            correct += cur * Math.pow(9, i);
        }

        return correct;
    }

    public static int getActualCost2(int cost) {
        if(cost < 4 ) {
            return cost;
        }

        if(cost <= 9) {
            return cost -1;
        }

        int correct = 0;
        int tmp = cost;
        List<Integer> list = new ArrayList<>();

        while (tmp > 0) {
            int yu = tmp % 10;
            list.add(yu);
            tmp = tmp / 10;
        }

        for (int i=0; i<list.size(); i++) {
            int cur = list.get(i);
            if (cur > 4) {
                cur--;
            }

            correct += cur * Math.pow(9, i);
        }

        return correct;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//        long k = scanner.nextLong();
//        long n = scanner.nextLong();
//        long m = scanner.nextLong();
//
//        long ans = howmuchLuckyNum(k, n, m);
//        System.out.println(ans);

        int cost = scanner.nextInt();
        System.out.println(getActualCost2(cost));
    }
}
